import myCarListDetailsZj from '@/components/myCarListDetailsZj.vue'
import defaultImg from '@/components/defaultImg/defaultImg.vue'
export default {
  components: {
    myCarListDetailsZj,
    defaultImg
  },
  data() {
    return {
      carllist: {}, //车子信息
      stiplist: [], //店铺信息
      carid: '', //车子id
      xedColorList: [], //颜色list

      specifAll: {
        brandlist: [], //品牌
        Serieslist: [], //系列
        Configulist: [], //车子
      },
      list: [],
      carList: {
        car_Type_Id: 3841, //车图片--系列id
      },
      isIcon: false, //删除按钮显示
      currentIndex: 0,
      id: 0,
      editname: '删除',
      derfimg: true, //是否显示默认图
      pageindex: 1,
      noInformation: false, //暂无信息
      defaultImg: false, //默认图
      isFixedTop: false,
      Topdistance: 3000,
      topHeight: '',
      lists: [{
        name: '方案',
        eg: 'a'
      },
      {
        name: '优势',
        eg: 'b'
      },
      {
        name: '流程',
        eg: 'c'
      },
      {
        name: '须知',
        eg: 'd'
      },
      {
        name: '售后服务',
        eg: 'e'
      }
      ],
      current: 0,
      lon: 0,
      lat: 0,
      showPage: false
    }
  },
  created() {},
  onLoad(caridxix) {
    this.lon = this.$store.state.lon
    this.lat = this.$store.state.lat
    // 选择品牌，系列，车子
    uni.$on('mycarlist', data => {
      Object.assign(this.specifAll, data)
      this.carList.car_Type_Id = 3841
      if (this.specifAll.Serieslist.carTypeId !== undefined) {
        this.carList.car_Type_Id = this.specifAll.Serieslist.carTypeId //系列id
      }
      if (data.Configulist && data.pageFrom === 'mycarlist') {
        this.InsertMyCarEntity() //添加车单
      }
    })
    //车子id
    // this.carid = 'd2521cc3d4134cefbf831eda8359d2b5'
    this.GetMyCarList() //获取车单列表

    // 吸顶
    uni.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    setTimeout(() => {
      this.GetTop()
      // this.aaa()
    }, 1000)
  },
  onUnload() {
    uni.$off('mycarlist')
  },
  onPageScroll(e) {
    // console.log(e.scrollTop)
    if (e.scrollTop > this.Topdistance) {
      this.isFixedTop = true
    } else {
      this.isFixedTop = false
    }
  },
  onShow() {
    let datatrue = uni.getStorageSync('Ztc_C_USER_INFO')
    this.$nextTick(() => {
      if (!datatrue.isNew) {
        // #ifdef APP-PLUS
        let webview = this.$mp.page.$getAppWebview()
        webview.setTitleNViewButtonStyle(0, {
          text: '',
          width: '0px'
        })
        // #endif
        // #ifdef H5
        let view = document.querySelectorAll('.uni-page-head-ft .uni-page-head-btn')
        console.log('view', view)
        view[0].childNodes[0].innerHTML = ''
        view[0].style.width = '0'
        // #endif
      }
    })
  },
  methods: {
    // 点击导航栏右侧
    onNavigationBarButtonTap(e) {
      /**
       * 向本地存储中设置selecerequirments的值，即启动标识；
       */
      uni.setStorage({
        key: 'selecerequirments',
        data: true,
      })
      uni.switchTab({
        url: '/pages/index/index'
      })
    },
    //添加车单
    async InsertMyCarEntity() {
      try {
        let data = {
          brand_Id: JSON.stringify(this.specifAll.brandlist.bid),
          brand_Name: this.specifAll.brandlist.brand,
          car_Type_Id: this.specifAll.Serieslist.carTypeId,
          car_Type_Name: this.specifAll.Serieslist.carTypeName,
          configuration_Id: this.specifAll.Configulist.id,
          configuration_Name: this.specifAll.Configulist.name
        }
        const res = await this.$api.commodity.InsertMyCarEntity(data)
        if (res.code === 200) {
          this.GetMyCarList() //获取车单
        }
      } catch (e) {}
    },
    //获取车单
    async GetMyCarList() {
      try {
        let data = {
          'pageIndex': this.pageindex,
          'pageSize': 10
        }
        const res = await this.$api.commodity.GetMyCarList(data)
        //如果总条数为0，说明没有车单，必须跳转品牌，选择车
        if (res.data.total === 0) {
          uni.navigateTo({
            url: '/pages_common/chooseSpecifications/chooseSpecifications?type=120&pageFrom=mycarlist'
          })
        } else {
          this.list = res.data.list
          // this.GetGoodsCarId(JSON.stringify(this.list[0].configuration_Id))
          this.GetCarLoanPaymentList(JSON.stringify(this.list[0].configuration_Id))

        }

        this.showPage = true
      } catch (e) {}
    },
    //根据车子匹配车源
    async GetGoodsCarId(id) {
      try {
        let data = {
          carid: id,
        }
        const res = await this.$api.commodity.GetGoodsCarId(data)
        if (res.data.id === null) {
          this.noInformation = false
          this.defaultImg = false
        } else {
          this.GetCarLoanPaymentList(res.data.id)
        }
      } catch (e) {}
    },
    //获取价格，店铺等
    async GetCarLoanPaymentList(id) {
      try {
        let data = {
          configuration_Id: id, //车辆ID
          lat: this.lat,
          lon: this.lon
        }
        const res = await this.$api.commodity.GetCarLoanPaymentList(data)
        if (res.data.id === null) {
          this.noInformation = false
          this.defaultImg = false
        } else {
          this.noInformation = true
          this.defaultImg = true
          // 所有信息 店铺信息 颜色信息
          this.$nextTick(() => {
            this.carid = res.data.goods_Carsource_Id
            this.stiplist = res.data.goodsCarSourceStoreDetailEntity
            this.carllist = res.data
            this.xedColorList = this.filtercolor(res.data.colorList) //颜色
          })
          this.xedColorList.forEach(item => {
            item.chekd = false
          })
        }
      } catch (e) {}
    },
    //图片报错
    imageError: function(e) {
      if (e.type === 'error') {
        // this.derfimg = false
        // <image src="https://files.yzsheng.com/client/comparison/images/che.png"></image>
      }
    },
    // 删除车单
    async delcars(e) {
      let data = {
        'id': e
      }
      const res = await this.$api.commodity.DeleteMyCar(data)
      this.GetMyCarList() //获取车单列表
      // this.editname = '完成'
    },
    //编辑
    async edit() {
      this.isIcon = !this.isIcon
      if (this.isIcon) {
        this.editname = '完成'
      } else {
        this.editname = '删除'
      }
    },
    //添加配置
    addConf() {
      uni.navigateTo({
        url: '/pages_common/chooseSpecifications/chooseSpecifications?type=120&pageFrom=mycarlist'
      })
    },
    //轮播定位
    swierChange(e) {
      this.carid = e.target.currentItemId
      this.GetCarLoanPaymentList(e.target.currentItemId) //获取价格，店铺等
      this.currentIndex = e.detail.current

    },
    trandstioncs(e) {
      this.pageindex = ++this.pageindex
      this.GetCarFullPaymentList()
      this.GetTop()
    },
    //分割颜色
    filtercolor(data) {
      data.forEach(item => {
        var arr = item.inColour.colorCoding.split('/')
        item.inColour.neiColor = arr
      })
      return data
    },
    //查看配置
    lookpeiclick() {
      uni.navigateTo({
        url: '/pages_commodity/payDeposit/payDeposit'
      })
    },
    GetTop() {
      //获取元素距离顶部的距离
      var _this = this
      uni.getSystemInfo({
        success: resu => {
          // console.log(resu)
          const query = uni.createSelectorQuery()
          query.select('#box').boundingClientRect()
          query.selectViewport().scrollOffset()
          query.exec(function(res) {
            _this.Topdistance = res[0].top
            // console.log(res)
          })
        },
        fail: res => {}
      })
    },
    //点击方案
    change(index) {
      // console.log(this.list[index].name)
      this.current = index
      //从评论区域回到顶部
      uni.createSelectorQuery().select('#progrn' + index).boundingClientRect(data => { //目标位置的节点：类或者id
        uni.createSelectorQuery().select('.arc-content').boundingClientRect(res => { //最外层盒子的节点：类或者id
          var number = data.top - res.top - -350
          // console.log(number)
          uni.pageScrollTo({
            duration: 200, //过渡时间
            scrollTop: number, //到达距离顶部的top值
          })
        }).exec()
      }).exec()
    },

    //查看配置
    //跳转车型参数--未完成
    lookpeiclick(item) {
      this.carllist.carid = item.configuration_Id
      this.carllist.car_Type_Name = item.car_Type_Name
      this.carllist.configuration_Name = item.configuration_Name
      this.carllist.car_ImageList = [{
        image: 'https://files.yzsheng.com/goodimg/cartypeimg/' + item.car_Type_Id + '.png!indexaskprice'
      }]
      uni.navigateTo({
        url: '/pages_common/productDcarConfigs/productDcarConfigs?carsList=' + encodeURIComponent(JSON.stringify(
          this.carllist))
      })
    },
  }
}
